﻿using Newtonsoft.Json;
using System.Net.Http;
using System.Text;
using System.Windows;
using JsonSerializer = System.Text.Json.JsonSerializer;

class Http_Network
{
    HttpClient client;
    //private string apiUrl = "https://192.168.11.106:7298/MyApi";
    //private string apiPostUrl = "https://192.168.11.106:7298/MyApi/Post";
    //private string apiPostUrl = "https://localhost:7119/Use";
    //string apiUrl = "https://localhost:7119/Use?TabName=ymh_Producedate&MachineName=雅马哈_1号机";
    public Http_Network()
    {
        var httpClientHandler = new HttpClientHandler();
        httpClientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) =>
        {
            return true;
        };
        client = new HttpClient(httpClientHandler);
    }

    private async Task HttpClientAsync(string apiUrl)
    {
        HttpResponseMessage httpRequest = await client.GetAsync(apiUrl);
        if (httpRequest.IsSuccessStatusCode)
        {
            string contents = await httpRequest.Content.ReadAsStringAsync();
        }
    }

    private async Task PostDataAsyncOne(string apiPostUrl)
    {
        var postData = new {id=0 };
        string jsonData = JsonSerializer.Serialize(postData);
        var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
        HttpResponseMessage response = await client.PostAsync(apiPostUrl, content);
        if (response.IsSuccessStatusCode)
        {
            string responseContent = await response.Content.ReadAsStringAsync();
        }
    }

    public async Task PostDataAsync(string apiPostUrl)
    {
        try
        {
            var postData = new
            {
                Command = "GetUserInfo",
            };
            string jsonData = JsonSerializer.Serialize(postData);
            var content = new StringContent(jsonData, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(apiPostUrl, content);
            if (response.IsSuccessStatusCode)
            {
                string responseContent = await response.Content.ReadAsStringAsync();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        
    }
}


